package com.parking.renters.rabbit;

import com.parking.common.core.model.RedisKey;
import com.parking.common.core.model.StateData;
import com.parking.common.web.util.RedisUtil;
import com.parking.common.web.util.SmsUtil;
import com.parking.renters.dao.RentersAccountLogsMapper;
import com.parking.renters.dao.RentersMapper;
import com.parking.renters.entity.Renters;
import com.parking.renters.entity.RentersAccountLogs;
import com.parking.renters.exception.RentersException;
import com.parking.renters.exceptionCode.RentersExceptionCode;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

@Component
public class RentersConsumer {
    //和队列相对应的处理消费者队列的
    @Resource
    private RentersAccountLogsMapper rentersAccountLogsMapper;
    @Resource
    private RentersMapper rentersMapper;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private SmsUtil smsUtil;


    /**
     *用户登录日志
     * map参数：
     * rentersLoginLogs：用户登录日志对象
     * */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "rentersLoginLogs")})
    private void rentersLoginLogs(Map<String,Object> map){
        RentersAccountLogs logs = (RentersAccountLogs) map.get("rentersLoginLogs");
        logs.setRentersLoginState(StateData.USER_LOGS_ISNOT_LOGIN);
        rentersAccountLogsMapper.insert(logs);
    }
    /**
     *用户注册日志
     * map参数：
     * rentersAddTime：出租客注册时间
     * */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "register")})
    private void register(Map<String,Object> map){
        Renters renters = (Renters)map.get("register");
        rentersMapper.updateById(renters);

    }
    /***
     * 出租客注册验证码
     * map参数：
     ("出租客电话")：phone;
     */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "sendRegisterCode")})
    private void sendRegisterCode(Map<String,Object> map) throws Exception {
        String phone = (String) map.get("phone");
        System.out.println(phone+"手机号");
        //获取一小时内短信数量
        String number = redisUtil.get(RedisKey.rentersCodeOnHour(phone));
        System.out.println(phone+"number");
        if(number!=null&&Integer.parseInt(number)==5){
            throw new RentersException(RentersExceptionCode.RENTERS_PHONE_FIVE_ONE);
        }
        //发送验证码
//        String code="123456";
        String code = smsUtil.send(phone);
        System.out.println(code+"验证码");
        //将验证码储存进redis
        redisUtil.set(RedisKey.rentersRegisterCode(phone),code,60*5L);
        System.out.println(redisUtil.get(RedisKey.rentersRegisterCode(phone))+"111111111111");;
        //验证码存储一分钟内是否发送验证码
        redisUtil.set(RedisKey.rentersRegisterCodeOnMinute(phone),code,60L);
        System.out.println("测试参数5"+redisUtil.get(RedisKey.rentersRegisterCode(phone)));
        System.out.println("测试参数5"+redisUtil.get(RedisKey.rentersRegisterCodeOnMinute(phone)));
        //当前用户的手机号验证码一小时内短信是否存在
        System.out.println(redisUtil.exist(RedisKey.sendNumbers(phone))+"111111111111");
        if (redisUtil.exist(RedisKey.sendNumbers(phone))) {
            redisUtil.incr(RedisKey.sendNumbers(phone));
        }else {
            redisUtil.set(RedisKey.sendNumbers(phone), String.valueOf(1),60*60L);
        }


    }
    /***
     *
     * 出租客登录日志
     * map参数：
     * ("出租客电话")：phone;
     *  ("出租客信息id")：rentersInfoId
     *   ("出租客账号")：rentersAccount
     */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "codeLogin")})
    private void codeLogin(Map<String,Object> map) throws Exception{

        Integer rentersAccountId = (Integer) map.get("rentersAccountId");
        String rentersAccountName = (String) map.get("rentersAccountName");
        LocalDateTime rentersLoginTime = (LocalDateTime)map.get("rentersLoginTime");
        RentersAccountLogs logs = new RentersAccountLogs();
        logs.setRentersAccountId(rentersAccountId);
        logs.setRentersLoginTime(rentersLoginTime);
        logs.setRentersLoginName(rentersAccountName);
        logs.setRentersLoginState(StateData.RENDERS_STATE_ENABLE);
        rentersAccountLogsMapper.insert(logs);


    }
    /***
     * 出租客账号修改记录
     * map参数：
     * rentersUpdateTime：修改时间;
     * rentersUpdatePerson：修改人
     */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "upStateById")})
    private void upStateById(Map<String,Object> map){
        Renters renters = (Renters) map.get("rentersAccount");
        rentersMapper.updateById(renters);

    }
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "sendLoginCode")})
    private void sendLoginCode(Map<String,Object> map) throws Exception{
        String phone = (String) map.get("phone");
        System.out.println(phone);
        //判断一小时内发送的验证码
        String number = redisUtil.get(RedisKey.rentersCodeOnHour(phone));
        System.out.println(number+"111111111111");
        if (number!=null&&Integer.parseInt(number)==5) {
            throw new RentersException(RentersExceptionCode.RENTERS_PHONE_FIVE_ONE);
        }
        //发送验证码
        String code = smsUtil.send(phone);
        System.out.println(code+"22222222222222222222");
//        String code="666666";
        //验证码存储登录信息
        redisUtil.set(RedisKey.rentersLoginCode(phone),code,5*60L);
        System.out.println(redisUtil.get(RedisKey.rentersLoginCode(phone))+"333333333333333");

        //验证码存储一分钟内是否发送验证码
        redisUtil.set(RedisKey.rentersLoginCodeOnMinute(phone),code,60L);
        System.out.println(redisUtil.get(RedisKey.rentersLoginCodeOnMinute(phone))+"44444444444");

        if (redisUtil.exist(RedisKey.sendNumbers(phone))) {
            redisUtil.incr(RedisKey.sendNumbers(phone));
        }else {
            redisUtil.set(RedisKey.sendNumbers(phone), String.valueOf(1),60*60L);
        }
    }

}
